//
//  ViewController.m
//  05-UIScrollView_Photo
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UIScrollView *smallScrollView;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatScrollView];
}

- (void)creatScrollView {
    NSArray *imageArr = @[@"0", @"1", @"2", @"3", @"4"];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.contentSize = CGSizeMake(WIDTH * imageArr.count, 0);
    [self.view addSubview:scrollView];
    
    for (int i = 0; i < 5; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        smallScrollView.minimumZoomScale = 1.0;
        smallScrollView.maximumZoomScale = 2.0;
        smallScrollView.tag = 1001;
        self.smallScrollView = smallScrollView;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", imageArr[i]]];
        [smallScrollView addSubview:imageView];
        [scrollView addSubview:smallScrollView];
    }
    
    UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH * 0.5, HEIGHT * 0.9, 50, 50)];
    self.page = page;
    page.numberOfPages = imageArr.count;
    [self.view addSubview:page];
    [page addTarget:self action:@selector(pageIndexChanged:) forControlEvents:UIControlEventValueChanged];

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    for (UIScrollView *sView in scrollView.subviews){
        if ([sView isKindOfClass:[UIScrollView class]]){
            sView.zoomScale = 1.0;
        }
    }
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
//    CGFloat x = scrollView.center.x;
//    CGFloat y = scrollView.center.y;
//    x = scrollView.contentSize.width > scrollView.center.x ? _smallScrollView.contentSize.width / 2 : x;
//    y = scrollView.contentSize.height > scrollView.center.y ? _smallScrollView.contentSize.height / 2 : y;
//    [scrollView setCenter:CGPointMake(x, y)];
}

- (void)pageIndexChanged:(UIPageControl *)page {
    self.scrollView.contentOffset = CGPointMake(page.currentPage * WIDTH, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.page.currentPage = scrollView.contentOffset.x / WIDTH;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
